﻿using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Model;
using Minecraft.Model.Dropinfo;
using Minecraft.Model.Treasurebox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall
{
	public static class FurnitureTreasureboxExt
	{
		/// <summary>
		/// 重置宝箱
		/// </summary>
		/// <param name="session"></param>
		/// <param name="isStochastic">如果在玩家账号创建时的随机宝箱生成，该值为 true，如果为预制，则为false</param>
		/// <param name="mapId"></param>
		public static void ResetTreasurebox(this MinecraftSession session, bool isStochastic, string mapId)
		{
			if (isStochastic) //随机
			{
				if (mapId != MinecraftConfig.BaseMapId)
				{
					TreasureboxGenarate(session, isStochastic, mapId);
				}
			}
			else //预制
			{
				TreasureboxGenarate(session, isStochastic, mapId);
			}
		}

		/// <summary>
		/// 宝箱生成算法
		/// </summary>
		/// <param name="session"></param>
		/// <param name="mapId"></param>
		private static void TreasureboxGenarate(MinecraftSession session, bool isStochastic, string mapId)
		{
			Random random = new Random(DateTimeHelper.GetTotalSecondsIntOfThisYear());
			//移除随机宝箱
			var treasureBoxStochasticList = session.minecraftSessionInfo.furnitureTreasureBoxList.Where(m => m.enumFurnitureType == EnumFurnitureType.Treasurebox && m.TreasureBox_MapId == mapId && m.TreasureBox_IsPrefab_Convert == false).ToList();
			session.minecraftSessionInfo.furnitureTreasureBoxList.RemoveAll(m => treasureBoxStochasticList.Any(k => m.FurnitureId == k.FurnitureId));
			session.minecraftSessionInfo.goodsList.RemoveAll(m => m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.Treasurebox && treasureBoxStochasticList.Any(k => k.FurnitureId == m.guid));

			//var csvMapInfo = CsvConfig.mapList.FirstOrDefault(m => m.Id == mapId);
			var csvMapInfo = CsvConfig.GetMod_MapInfoById(mapId);

			if (!isStochastic) //预制
			{
				////预制宝箱列表
				////var treasureBoxPrefabList = CsvConfig.mapresourceList.Where(m => m.MapID == mapId).ToList();
				//var treasureBoxPrefabList = CsvConfig.GetmapresourceDict().Where(m => m.Value.MapId == mapId).Select(m => m.Value).ToList();
				//if (treasureBoxPrefabList == null || !treasureBoxPrefabList.Any())
				//{
				//	return;
				//	throw new Exception($"指定地图的预制宝箱信息个数为0【mapId：{mapId}】");
				//}
				////预制宝箱个数
				//foreach (var treasureBoxPrefab in treasureBoxPrefabList)
				//{
				//	//var csvTreasureInfo = CsvConfig.furnitureList.FirstOrDefault(m => m.Id == treasureBoxPrefab.BlockEntityID);
				//	var csvTreasureInfo = CsvConfig.GetMod_DrawingInfoById(treasureBoxPrefab.BlockEntityId);
				//	if (csvTreasureInfo == null)
				//	{
				//		throw new Exception("获取 csvTreasureInfo 失败，可能配置表不存在该宝箱，请请联系策划");
				//	}
				//	//var dropInfo = CsvConfig.dropList.FirstOrDefault(m => m.Id == csvTreasureInfo.Drop);
				//	var dropInfo = CsvConfig.GetMod_DropInfoById(csvTreasureInfo.Drop);
				//	if (dropInfo == null)
				//	{
				//		throw new Exception("获取 dropInfo 失败");
				//	}

				//	List<TreasureMaterialGoodsInfo> treasureMaterialGoodsInfoList = new List<TreasureMaterialGoodsInfo>();

				//	List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList = session.GetDropinfoDropRandomResourceInfoList(dropInfo.Id);
				//	for (int i = 0; i < dropinfoDropResourceInfoList.Count(); i++)
				//	{
				//		var dropinfoDropResourceInfo = dropinfoDropResourceInfoList[i];
				//		treasureMaterialGoodsInfoList.Add(new TreasureMaterialGoodsInfo
				//		{
				//			GoodsItemId = dropinfoDropResourceInfo.GoodsItemId,
				//			Num = dropinfoDropResourceInfo.Num,
				//			GoodsPosition = i
				//		});
				//	}

				//	var furnitureTreasureBox = new FurnitureModel
				//	{
				//		FurnitureId = session.GetNewFurnitureId(),
				//		FurnitureItemId = csvTreasureInfo.Id,
				//		FurnitureTypeId = (int)EnumFurnitureType.Treasurebox,
				//		PlayerId = session.minecraftSessionInfo.player.PlayerId,
				//		TreasureBox_MapId = csvMapInfo.Id,
				//		TreasureBox_IsPrefab = 1,
				//		Point_X = treasureBoxPrefab.point3.x,
				//		Point_Y = treasureBoxPrefab.point3.y,
				//		Point_Z = treasureBoxPrefab.point3.z,
				//		angle = treasureBoxPrefab.Radius_Convert,
				//	};
				//	session.minecraftSessionInfo.furnitureTreasureBoxList.Add(furnitureTreasureBox);
				//	foreach (var treasureMaterialGoodsInfo in treasureMaterialGoodsInfoList)
				//	{
				//		session.minecraftSessionInfo.goodsList.Add(new GoodsModel
				//		{
				//			GoodsId = StringHelper.GetGuidStr(),
				//			BelongsTo = (int)EnumGoodsBelongsToType.Treasurebox,
				//			GoodsItemId = treasureMaterialGoodsInfo.GoodsItemId,
				//			GoodsPosition = treasureMaterialGoodsInfo.GoodsPosition,
				//			guid = furnitureTreasureBox.FurnitureId,
				//			PlayerId = session.minecraftSessionInfo.player.PlayerId,
				//			WastageValue = 100,
				//			Num = treasureMaterialGoodsInfo.Num,
				//		});
				//	}
				//}
			}
			else //随机
			{
				var csvMapResourceList = CsvConfig.GetmapresourceDict().Select(m => m.Value).Where(m => m.MapId == csvMapInfo.Id).ToList();
				var mapResourceList = new List<Mod_MapResource>();
				var maxCount = Math.Max(csvMapInfo.TreasureBoxMaxNum_Convert, csvMapResourceList.Count());
				if (maxCount <= csvMapInfo.TreasureBoxMaxNum_Convert)
				{
					mapResourceList = csvMapResourceList.DeepCopy();
				}
				else
				{
					//随机生成（筛选）（获取下标列表）
					List<int> indexRandomList = new List<int>();
					ListHelper.RandomNotEqualList(ref indexRandomList, 0, csvMapResourceList.Count() - 1, csvMapInfo.TreasureBoxMaxNum_Convert);
					List<int> resourceIdList = new List<int>();
					for (int i = 0; i < indexRandomList.Count(); i++)
					{
						resourceIdList.Add(csvMapResourceList[indexRandomList[i]].ResourceId_Convert);
					}
					mapResourceList = csvMapResourceList.Where(m => resourceIdList.Any(k => k == m.ResourceId_Convert)).ToList();
				}
				//宝箱个数
				foreach (var mapResource in mapResourceList)
				{
					var mapDropInfoList = csvMapInfo.GetModMapTreasureBoxDropInfoList();
					var rateSum = mapDropInfoList.Sum(m => m.rate);
					var rateRangeVal = random.Next(1, rateSum + 1);
					var mapDropInfo = mapDropInfoList.FirstOrDefault(m => m.rateMinVal <= rateRangeVal && m.rateMaxVal >= rateRangeVal);
					if (mapDropInfo == null)
					{
						throw new Exception("获取 mapDropInfo 失败");
					}
					//var csvTreasureInfo = CsvConfig.furnitureList.FirstOrDefault(m => m.Id == mapDropInfo.treasureBoxItemId);
					var csvTreasureInfo = CsvConfig.GetMod_DrawingInfoById(mapDropInfo.treasureBoxItemId);
					if (csvTreasureInfo == null)
					{
						throw new Exception("获取 csvTreasureInfo 失败，可能配置表不存在该宝箱，请请联系策划");
					}
					//var dropInfo = CsvConfig.dropList.FirstOrDefault(m => m.Id == csvTreasureInfo.Drop);
					var dropInfo = CsvConfig.GetMod_DropInfoById(csvTreasureInfo.Drop);
					if (dropInfo == null)
					{
						throw new Exception("获取 dropInfo 失败");
					}

					List<TreasureMaterialGoodsInfo> treasureMaterialGoodsInfoList = new List<TreasureMaterialGoodsInfo>();

					List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList = session.GetDropinfoDropRandomResourceInfoList(dropInfo.Id);
					for (int j = 0; j < dropinfoDropResourceInfoList.Count(); j++)
					{
						var dropinfoDropResourceInfo = dropinfoDropResourceInfoList[j];
						treasureMaterialGoodsInfoList.Add(new TreasureMaterialGoodsInfo
						{
							GoodsItemId = dropinfoDropResourceInfo.GoodsItemId,
							Num = dropinfoDropResourceInfo.Num,
							GoodsPosition = j
						});
					}

					var furnitureTreasureBox = new FurnitureModel
					{
						FurnitureId = session.GetNewFurnitureId(),
						FurnitureItemId = csvTreasureInfo.Id,
						FurnitureTypeId = (int)EnumFurnitureType.Treasurebox,
						PlayerId = session.minecraftSessionInfo.player.PlayerId,
						TreasureBox_MapId = csvMapInfo.Id,
						TreasureBox_IsPrefab = 0,
						Point_X = mapResource.point3.x,
						Point_Y = mapResource.point3.y,
						Point_Z = mapResource.point3.z,
						angle = mapResource.Radius_Convert,
						TreasureBox_ResourceId = mapResource.ResourceId_Convert,
						TreasureBox_IsGenerate = 0,
						TreasureBox_IsOpen = 0,
					};
					session.minecraftSessionInfo.furnitureTreasureBoxList.Add(furnitureTreasureBox);
					foreach (var treasureMaterialGoodsInfo in treasureMaterialGoodsInfoList)
					{
						session.minecraftSessionInfo.goodsList.Add(new GoodsModel
						{
							GoodsId = StringHelper.GetGuidStr(),
							BelongsTo = (int)EnumGoodsBelongsToType.Treasurebox,
							GoodsItemId = treasureMaterialGoodsInfo.GoodsItemId,
							GoodsPosition = treasureMaterialGoodsInfo.GoodsPosition,
							guid = furnitureTreasureBox.FurnitureId,
							PlayerId = session.minecraftSessionInfo.player.PlayerId,
							WastageValue = 100,
							Num = treasureMaterialGoodsInfo.Num
						});
					}
				}
				////随机宝箱个数
				//int treasureboxCount = random.Next(csvMapInfo.TreasureBoxMaxNum_Convert + 1);
				//for (int i = 0; i < treasureboxCount; i++)
				//{
				//	var mapDropInfoList = csvMapInfo.GetModMapTreasureBoxDropInfoList();
				//	var rateSum = mapDropInfoList.Sum(m => m.rate);
				//	var rateRangeVal = random.Next(1, rateSum + 1);
				//	var mapDropInfo = mapDropInfoList.FirstOrDefault(m => m.rateMinVal <= rateRangeVal && m.rateMaxVal >= rateRangeVal);
				//	if (mapDropInfo == null)
				//	{
				//		throw new Exception("获取 mapDropInfo 失败");
				//	}
				//	//var csvTreasureInfo = CsvConfig.furnitureList.FirstOrDefault(m => m.Id == mapDropInfo.treasureBoxItemId);
				//	var csvTreasureInfo = CsvConfig.GetMod_DrawingInfoById(mapDropInfo.treasureBoxItemId);
				//	if (csvTreasureInfo == null)
				//	{
				//		throw new Exception("获取 csvTreasureInfo 失败，可能配置表不存在该宝箱，请请联系策划");
				//	}
				//	//var dropInfo = CsvConfig.dropList.FirstOrDefault(m => m.Id == csvTreasureInfo.Drop);
				//	var dropInfo = CsvConfig.GetMod_DropInfoById(csvTreasureInfo.Drop);
				//	if (dropInfo == null)
				//	{
				//		throw new Exception("获取 dropInfo 失败");
				//	}

				//	List<TreasureMaterialGoodsInfo> treasureMaterialGoodsInfoList = new List<TreasureMaterialGoodsInfo>();

				//	List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList = session.GetDropinfoDropRandomResourceInfoList(dropInfo.Id);
				//	for (int j = 0; j < dropinfoDropResourceInfoList.Count(); j++)
				//	{
				//		var dropinfoDropResourceInfo = dropinfoDropResourceInfoList[j];
				//		treasureMaterialGoodsInfoList.Add(new TreasureMaterialGoodsInfo
				//		{
				//			GoodsItemId = dropinfoDropResourceInfo.GoodsItemId,
				//			Num = dropinfoDropResourceInfo.Num,
				//			GoodsPosition = j
				//		});
				//	}

				//	var furnitureTreasureBox = new FurnitureModel
				//	{
				//		FurnitureId = session.GetNewFurnitureId(),
				//		FurnitureItemId = csvTreasureInfo.Id,
				//		FurnitureTypeId = (int)EnumFurnitureType.Treasurebox,
				//		PlayerId = session.minecraftSessionInfo.player.PlayerId,
				//		TreasureBox_MapId = csvMapInfo.Id,
				//		TreasureBox_IsPrefab = 0,
				//	};
				//	session.minecraftSessionInfo.furnitureTreasureBoxList.Add(furnitureTreasureBox);
				//	foreach (var treasureMaterialGoodsInfo in treasureMaterialGoodsInfoList)
				//	{
				//		session.minecraftSessionInfo.goodsList.Add(new GoodsModel
				//		{
				//			GoodsId = StringHelper.GetGuidStr(),
				//			BelongsTo = (int)EnumGoodsBelongsToType.Treasurebox,
				//			GoodsItemId = treasureMaterialGoodsInfo.GoodsItemId,
				//			GoodsPosition = treasureMaterialGoodsInfo.GoodsPosition,
				//			guid = furnitureTreasureBox.FurnitureId,
				//			PlayerId = session.minecraftSessionInfo.player.PlayerId,
				//			WastageValue = 100,
				//			Num = treasureMaterialGoodsInfo.Num
				//		});
				//	}
				//}
			}
		}
	}
}
